package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqut.dto.LimitShowDTO;
import com.cqut.model.EvaluationDetails;
import com.cqut.model.EvaluationEd;
import com.cqut.dto.EvaluationDetailsDTO;
import com.cqut.dto.EvaluationEdDTO;
import com.cqut.dto.EvaluationEdNoTimeDTO;
import com.cqut.util.ResponseUtil;
import com.cqut.service.IEvaluationEdService;


@RestController
@RequestMapping("/api/evaluationEd")
public class EvaluationEdController {
    @Resource(name="evaluationEdService")
    private IEvaluationEdService evaluationEdService;

    /**
     * 得到评审标准表
     * @param editionName
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
    	@RequestParam("editionName")
        String editionName,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> evaluationEdList = evaluationEdService.getEvaluationEdList(editionName,limitShowDTO);
        long count = evaluationEdService.getEvaluationEdCount(editionName);

        result.put("rows", evaluationEdList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    /**
     * 新增版本
     * **/  @RequestMapping(value = "/addVersion", method = RequestMethod.POST)
     public ResponseEntity<String> addNewVersion(
    		 @Validated
    		 EvaluationEdDTO evaluationEdDTO, BindingResult errors){
    	  if(errors.hasErrors())
              return ResponseUtil.getResponseEntityWhenInvalidReqParams();
    	  
    	  EvaluationEd evaluationEdModel = evaluationEdDTO.toModel();
         
         return ResponseUtil.getResEntityForPPP(evaluationEdService.addNewVersion(evaluationEdModel));
     }
   
     
    @RequestMapping(value = "/{evaluationId}", method = RequestMethod.GET)
    public ResponseEntity<String> show(@PathVariable("evaluationId") String evaluationId) {
        Map<String, Object> evaluationEdModel = evaluationEdService.findMap(evaluationId);

        return ResponseUtil.getResEntityForGetAndJson(evaluationEdModel);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        EvaluationEdDTO evaluationEdDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        EvaluationEd evaluationEdModel = evaluationEdDTO.toModel();

        return ResponseUtil.getResEntityForPPP(evaluationEdService.save(evaluationEdModel));
    }

    @RequestMapping(value = "/{evaluationId}", method = RequestMethod.PUT)
    public ResponseEntity<String> update(@PathVariable("evaluationId") String evaluationId,
        @Validated
        EvaluationEdDTO evaluationEdDTO, BindingResult errors) {

        EvaluationEd evaluationEdModel = evaluationEdDTO.toModel();
        evaluationEdModel.setEvaluationId(evaluationId);
        return ResponseUtil.getResEntityForPPP(evaluationEdService.update(evaluationEdModel));
    }

    @RequestMapping(value = "/{evaluationId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory(@PathVariable("evaluationId") String evaluationId) {
        return ResponseUtil.getResEntityForDel(evaluationEdService.delete(evaluationId));
    }

    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public ResponseEntity<String> deletes(@RequestParam("primaryKeys") String[] primaryKeys) {
        return ResponseUtil.getResEntityForDel(evaluationEdService.batchDelete(primaryKeys));
    }
    
    /**
	 *	获取版本
	 **/
	@RequestMapping(value = "/getEvaluation", method = RequestMethod.POST)
	public ResponseEntity<String> getEvaluation(@RequestParam("evaluationId") String evaluationId) {
		System.out.println(evaluationId);
		Map<String, Object> map = evaluationEdService.getEvaluationById(evaluationId);
		return ResponseUtil.getResEntityForGetAndJson(map);
	}
	/**
	 * 修改版本
	 **/
	@RequestMapping(value = "/updateEvaluation", method = RequestMethod.POST)
	public ResponseEntity<String> update(@Validated EvaluationEdDTO evaluationEdDTO, BindingResult errors) {
		if (errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();

		EvaluationEd evaluationEdModel = evaluationEdDTO.toModel();
		
		return ResponseUtil.getResEntityForPPP(evaluationEdService.updateEvaluation(evaluationEdModel));
	}
}
